"use strict";

var myApp = angular.module('myApp.controllers',[]);

function getTodayDate() {
	var today = new Date();
	var todayString = today.getFullYear()+'-';
	if (today.getMonth() < 9) {todayString+='0'}
	todayString += (today.getMonth()+1)+'-';
	if (today.getDate() < 10) {todayString+='0'}
	todayString += today.getDate();
	return todayString;
};

//myApp.controller('ConsultCtrl',function($scope, $http, $location, $routeParams) {
function ConsultCtrl($scope, $http, $location, $routeParams,PatientServices,MedecinServices) {
	$scope.block = true;
	
	$scope.addConsultation = function(block,title,contenu) {
		$scope.patient.consultations.push({block : block, titre : title, date : getTodayDate(),contenu: contenu});
	};
	
	$scope.savePatient = function(patient) {
		PatientServices.saveData(patient,function(status){
			if (status!=200) alert("Un probleme est survenu lors de la sauvegarde du patient");
			$scope.patient = PatientServices.getGlobalPatient();
		});
	};
	
	$scope.getPatient = function() {
		if (!PatientServices.getGlobalPatient()) {
			updateWorkingOn("patient-consult",PatientServices,MedecinServices);	
			return;
		}
		PatientServices.readData(PatientServices.getGlobalPatient(),function(data,status){
			(status==200)?$scope.patient=data:alert("Un probleme est survenu lors de la recuperation du patient"); 
			updateWorkingOn("patient-consult",PatientServices,MedecinServices);
		});
	};
	
	$scope.updateConsult = function(consultation) {
		PatientServices.setGlobalPatient($scope.patient);
	}
	
	$scope.removeConsultation = function(consultation) {
		var index = $scope.patient.consultations.indexOf(consultation);
		if (index > -1) {
			$scope.patient.consultations.splice(index,1);
		}
		PatientServices.setGlobalPatient($scope.patient);
	}
	
	$scope.printFicheBloc = function (){
		var win = window.open('','','left=0,top=0,width=1024px,height=748px,toolbar=0,scrollbars=0,status =0');

		var content = "<html>";
		content += "<body onload=\"window.print(); window.close();\">";
		content += document.getElementById("fiche_block").innerHTML ;
		content += "</body>";
		content += "</html>";
		win.document.write(content);
		win.document.close();
	}

	$scope.getPatient();

};

